import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class BackgroundImageExample extends JFrame {
    private JPanel settingsPanel;
    private JPanel attendancePanel;
    private JPanel monitoringPanel;

    public BackgroundImageExample() {
        setTitle("管理系统");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(1000, 600);
        setLocationRelativeTo(null);

        // 创建选项卡面板
        JTabbedPane tabbedPane = new JTabbedPane();

        // 设置模块
        settingsPanel = createPanelWithBackground("setting-background.jpg");
        addButtons(settingsPanel);
        tabbedPane.addTab("设置", settingsPanel);

        // 考勤模块
        attendancePanel = createPanelWithBackground("attendance-background.jpg");
        addButtons(attendancePanel);
        tabbedPane.addTab("考勤", attendancePanel);

        // 监控模块
        monitoringPanel = createPanelWithBackground("monitoring-background.jpg");
        addButtons(monitoringPanel);
        tabbedPane.addTab("监控", monitoringPanel);

        add(tabbedPane);
        setVisible(true);
    }

    private JPanel createPanelWithBackground(String imagePath) {
        JPanel panel = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                try {
                    Image image = new ImageIcon(imagePath).getImage();
                    g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
        panel.setLayout(new GridBagLayout()); // 使用GridBagLayout布局管理器
        return panel;
    }

    private void addButtons(JPanel panel) {
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(20, 20, 20, 20); // 设置按钮之间的间距

        for (int i = 0; i < 4; i++) {
            JButton button = createButton("按钮 " + (i + 1));
            button.setPreferredSize(new Dimension(300, 300));
            panel.add(button, gbc);
        }
    }

    private JButton createButton(String label) {
        JButton button = new JButton(label) {
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(300, 300); // 设置按钮的大小为正方形
            }
        };
        button.setBackground(Color.BLUE);
        button.setForeground(Color.WHITE); // 设置文字颜色为白色
        button.setFocusPainted(false); // 去除按钮的焦点框
        button.setFont(new Font("Arial", Font.BOLD, 20)); // 设置字体和大小
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(button, "点击了 " + label);
            }
        });
        return button;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            new BackgroundImageExample();
        });
    }
}
